﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using BenevolentSun;

namespace Editor
{
    class SaveConfirmScreen : Screen
    {

        public event EventHandler OnSave;

        public event EventHandler OnSaveAndClose;

        public event EventHandler OnCancel;


        SpriteFont font;

        List<String> menu = new List<string>();

        int selectedMenuEntry = 0;


        public SaveConfirmScreen()
        {
            menu.Add("Cancel");
            menu.Add("Save");
            menu.Add("Save and Close");
            menu.Add("Exit without Saving");
        }


        public override void HandleInput()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadDown) ||
                InputManager.IsNewKeyPress(Keys.Down))
            {
                selectedMenuEntry++;
            }
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadUp) ||
                InputManager.IsNewKeyPress(Keys.Up))
            {
                selectedMenuEntry--;
            }
            selectedMenuEntry = Math.Max(selectedMenuEntry, 0);
            selectedMenuEntry = Math.Min(selectedMenuEntry, menu.Count - 1);

            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.A) ||
                InputManager.IsNewKeyPress(Keys.Enter))
            {
                switch (selectedMenuEntry)
                {
                    case 0:
                        if (OnCancel != null)
                            OnCancel(this, new EventArgs());
                        ExitScreen();
                        break;
                    case 1:
                        if (OnSave != null)
                            OnSave(this, new EventArgs());
                        ExitScreen();
                        break;
                    case 2:
                        if (OnSaveAndClose != null)
                            OnSaveAndClose(this, new EventArgs());
                        ExitScreen();
                        break;
                    case 3:
                        ScreenManager.Game.Exit();
                        break;
                }
            }

            base.HandleInput();
        }


        public override void LoadContent()
        {
            font = Content.Load<SpriteFont>("Fonts/Ebrima14");
            
            base.LoadContent();
        }


        public override void Draw(GameTime gameTime)
        {

            this.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
            
            SpriteBatch.DrawString(font, "What would you like to do?", new Vector2(20f, 20f), Color.Yellow);

            for (int i = 0; i < menu.Count; ++i)
            {
                Color color = new Color(51, 51, 51, (byte)(255));
                if (i == selectedMenuEntry)
                    color = new Color(204, 204, 204, (byte)(255));
                SpriteBatch.DrawString(font, menu[i], new Vector2(30, 40 + i * 20), color);
            }

            SpriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
